package com.b2b.controller.system;

import com.b2b.model.SystemAdministratorDto;
import com.b2b.service.SystemApiAuthorityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.SystemApiAuthorityDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/systemApiAuthority")
@PropertySource("classpath:config.properties")
public class SystemApiAuthorityController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final SystemApiAuthorityService systemApiAuthorityService;

    public SystemApiAuthorityController(SystemApiAuthorityService systemApiAuthorityService){
        this.systemApiAuthorityService = systemApiAuthorityService;
    }
    /**
     * 新增API权限
     * @param systemApiAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PostMapping("/add")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add(@RequestBody SystemApiAuthorityDto systemApiAuthorityDto) {
        // 判断当前名称是否存在
        SystemApiAuthorityDto systemApiAuthorityDtoTemp = new SystemApiAuthorityDto();
        systemApiAuthorityDtoTemp.setApiName(systemApiAuthorityDto.getApiName());
        Integer count = systemApiAuthorityService.selectCount(systemApiAuthorityDtoTemp);
        if (count <= 0 ){
            throw new CustomUnauthorizedException("该账号不存在(Account Not exist.)");
        }
        count = systemApiAuthorityService.insert(systemApiAuthorityDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", systemApiAuthorityDto);
    }

    @GetMapping("/getTree")
    public ResponseBean findTree(){
        List<SystemApiAuthorityDto> systemApiAuthorityDtos = systemApiAuthorityService.selectAll();
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", systemApiAuthorityDtos);
    }
    /**
     * 获取API权限
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/getAll")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findAll(BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<SystemApiAuthorityDto> systemApiAuthorityDtos = systemApiAuthorityService.selectAll();
        PageInfo<SystemApiAuthorityDto> selectPage = new PageInfo<SystemApiAuthorityDto>(systemApiAuthorityDtos);
        if (systemApiAuthorityDtos == null || systemApiAuthorityDtos.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
    /**
     * 获取指定API
     * @param systemApiAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/get")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(SystemApiAuthorityDto systemApiAuthorityDto) {
        systemApiAuthorityDto = systemApiAuthorityService.selectByPrimaryKey(systemApiAuthorityDto.getApiId());
        if (systemApiAuthorityDto == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", systemApiAuthorityDto);
    }
    /**
     * 更新API权限
     * @param systemApiAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody SystemApiAuthorityDto systemApiAuthorityDto) {
        SystemApiAuthorityDto systemApiAuthorityDtoTemp = new SystemApiAuthorityDto();
        systemApiAuthorityDtoTemp.setApiId(systemApiAuthorityDto.getApiId());
        Integer count = systemApiAuthorityService.selectCount(systemApiAuthorityDtoTemp);
        if (count <= 0 ){
            throw new CustomUnauthorizedException("该账号不存在(Account Not exist.)");
        }
        count = systemApiAuthorityService.updateByPrimaryKeySelective(systemApiAuthorityDto);

        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", systemApiAuthorityDto);
    }

    /**
     * 删除相关公司API权限
     * @param apiId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/delete")
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestParam Integer apiId) {
        SystemApiAuthorityDto systemApiAuthorityDto = new SystemApiAuthorityDto();
        systemApiAuthorityDto.setApiId(apiId);
        Integer count = systemApiAuthorityService.selectCount(systemApiAuthorityDto);
        if (count <= 0 ){
            throw new CustomUnauthorizedException("该账号不存在(Account Not exist.)");
        }
        count = systemApiAuthorityService.deleteByPrimaryKey(apiId);
        if (count <= 0 ){
            throw new CustomUnauthorizedException("删除失败(Delete Fail)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

}
